import pgzrun
import time
import random
WIDTH = 800
HEIGHT = 1000
BACK_COLOR = (0, 144, 103)
board = [[0, 0, 0, 0, 0, 0, 0, 0],
         [0, 0, 0, 0, 0, 0, 0, 0],
         [0, 0, 0, 3, 0, 0, 0, 0],
         [0, 0, 3, 1, 2, 0, 0, 0],
         [0, 0, 0, 2, 1, 3, 0, 0],
         [0, 0, 0, 0, 3, 0, 0, 0],
         [0, 0, 0, 0, 0, 0, 0, 0],
         [0, 0, 0, 0, 0, 0, 0, 0]]

score = [[900, -80, 10, 10, 10, 10, -80, 900],
         [-80, -80, -3, -3, -3, -3, -80, -80],
         [10, -3, 3, 3, 3, 3, -3, 10],
         [10, -3, 3, 1, 1, 3, -3, 10],
         [10, -3, 3, 1, 1, 3, -3, 10],
         [10, -3, 3, 3, 3, 3, -3, 10],
         [-80, -80, -3, -3, -3, -3, -80, -80],
         [900, -80, 10, 10, 10, 10, -80, 900], ]

w = 8
h = 8
imgs = 100
side = 1
end = 0
spstr = ""
Aiside = 0
start = 1
show = 0


def AI():
    global side, board
    can_put = []
    Max = -10000
    M_p = (0, 0)
    M_l = []
    for i in range(w):
        for j in range(h):
            if(board[i][j] == 3):
                can_put.append((i, j))
    random.shuffle(can_put)
    if(random.randint(0, 10) == 1):
        a = random.randint(0, len(can_put)-1)
        M_p = can_put[a]
        M_l = check_down(can_put[a])
    else:
        for i in can_put:
            l = check_down(i)
            s = sum([score[x[0]][x[1]] for x in l]+[score[i[0]][i[1]]])
            if(s > Max):
                Max = s
                M_p = i
                M_l = l
    x, y = M_p[0], M_p[1]
    board[x][y] = side+1
    for i in M_l:
        board[i[0]][i[1]] = side+1
    side = not side
    check_may()


def init():
    global board, side, end, spstr, start, show
    board = [[0, 0, 0, 0, 0, 0, 0, 0],
             [0, 0, 0, 0, 0, 0, 0, 0],
             [0, 0, 0, 3, 0, 0, 0, 0],
             [0, 0, 3, 1, 2, 0, 0, 0],
             [0, 0, 0, 2, 1, 3, 0, 0],
             [0, 0, 0, 0, 3, 0, 0, 0],
             [0, 0, 0, 0, 0, 0, 0, 0],
             [0, 0, 0, 0, 0, 0, 0, 0]]
    side = 1
    end = 0
    spstr = ""
    start = 1
    show = 0


def check_list(sx, sy, dx, dy):
    n = []
    while(0 <= sx < w and 0 <= sy < h):
        if(board[sx][sy] == 0 or board[sx][sy] == 3):
            return []
        if(board[sx][sy] == side+1):
            return n
        n.append((sx, sy))
        sx += dx
        sy += dy
    return []


def check_down(pos):
    x, y = pos[0], pos[1]
    if(board[x][y] != 3 and board[x][y] != 0):
        return []
    del_list = []
    del_list += check_list(x+1, y, 1, 0)  # ->
    del_list += check_list(x-1, y, -1, 0)  # <-
    del_list += check_list(x, y+1, 0, 1)  # down
    del_list += check_list(x, y-1, 0, -1)  # up
    del_list += check_list(x-1, y-1, -1, -1)  # \up
    del_list += check_list(x+1, y+1, 1, 1)  # \down
    del_list += check_list(x+1, y-1, 1, -1)  # /up
    del_list += check_list(x-1, y+1, -1, 1)  # /down
    return del_list


def check_win():
    global spstr
    bl = 0
    wh = 0
    for i in range(w):
        for j in range(h):
            if(board[i][j] == 1):
                wh += 1
            elif(board[i][j] == 2):
                bl += 1
    if(bl > wh):
        spstr = "black win"
    elif(wh > bl):
        spstr = "white win"
    else:
        spstr = "draw"


def check_may():
    global side, board, end
    n = 0
    num = 0
    for i in range(w):
        for j in range(h):
            m = len(check_down((i, j)))
            if(m != 0):
                board[i][j] = 3
                n += 1
            elif(board[i][j] == 3):
                board[i][j] = 0
            if(board[i][j] == 1 or board[i][j] == 2):
                num += 1
    if(n == 0 or num == w*h):
        side = not side
        end += 1
        if(end == 2 or num == w*h):
            side = -1
            check_win()
            return
        check_may()
    else:
        end = 0


def update():
    global show
    if(start == 0 or spstr != ''):
        return
    if(show != 1):
        show = 1
        return
    AI()
    show = 0


def draw():
    screen.clear()
    screen.fill(BACK_COLOR)
    bl = 0
    wh = 0
    for i in range(w):
        for j in range(h):
            n = 0
            if(board[i][j] == 2):
                n = Actor('black', pos=(i*imgs+imgs//2, j*imgs+imgs//2))
                bl += 1
            elif(board[i][j] == 1):
                n = Actor('white', pos=(i*imgs+imgs//2, j*imgs+imgs//2))
                wh += 1
            elif(board[i][j] == 3):
                n = Actor('may', pos=(i*imgs+imgs//2, j*imgs+imgs//2))
            else:
                n = Actor('none', pos=(i*imgs+imgs//2, j*imgs+imgs//2))
            n.draw()
    screen.blit("background", (0, 800))
    if(side == 0):
        screen.draw.text("white's turn", center=(400, 827), fontsize=37)
    elif(spstr != ''):
        screen.draw.text(spstr, center=(400, 827), fontsize=37, color="red")
    else:
        screen.draw.text("black's turn", center=(
            400, 827), fontsize=37, color="black")
    screen.draw.text("white "+str(wh), midleft=(45, 900), fontsize=120)
    screen.draw.text(str(bl)+" black", midright=(755, 900),
                     fontsize=120, color="black")
    if(start):
        time.sleep(1.0)


pgzrun.go()
